<?php
namespace Admin\Model;

class GoodsModel extends CommonModel{
	protected $fields = array("id","goods_name","goods_sn","cate_id","market_price","shop_price","goods_img","goods_thumb","is_hot","is_rec","is_new","addtime","is_del","is_sale","goods_body","type_id");
	protected $_validate = array(
		//array(验证字段1,验证规则,错误提示,[验证条件,附加规则,验证时间]),
		array("goods_name","require","商品名称必填",1),
		array("market_price","currency","市场价格必填",1),
		array("shop_price","currency","本店价格必填"),
		array("cate_id","check_cate","商品分类必填",1,"callback"),
	);
	public function check_cate($cate_id){
		$cate_id = intval($cate_id);
		if($cate_id>0){
			return true;
		}
		return false;
	}
	public function _before_insert(&$data){
		//钩子函数
		$data["addtime"] = time();

		//处理货号
		 if(!$data["goods_sn"]){
		 	$data["goods_sn"] = 'huohao'.uniqid();
		 }else{
		 	//有提交货号
		 	$info = $this->where("goods_sn=".$data["goods_sn"])->find();
		 	if($info){
		 		$this->error = "货号重复";
		 		return false;
		 	}
		 }
		$res = $this->uploadImg();
		if($res){
			$data["goods_img"] = $res["goods_img"];
			$data["goods_thumb"] = $res["thumb_goods_img"];
		}
	}
	//商品扩展分类的入库  钩子函数获取数据入库
	//_before_insert 添加之前
	//_after_insert 添加之后
	public function _after_insert($data){
		//扩展分类入库
		$goods_id = $data["id"];
		$cate_id = I("post.cate_ext");
		D("GoodsCate")->insertExtCate($goods_id,$cate_id);
	}

	public function listData($is_del=0){
		//1.定义每页显示的数据条数
		//返回数据和分页导航数据
		$pageSize = 20;

		$condition = "is_del=".$is_del;
		//分类搜索
		$cate_id = intval(I("post.cate_condition"));

		if($is_sale && $is_sale>0){
			if($is_sale == 1){
				$condition .= " AND is_sale=1";
			}else{
				$condition .= " AND is_sale=0";
			}
		}
		if($cate_id && $cate_id>0){
			//1.根据分类id查找商品
			//2.根据分类id以及下属的分类查找商品
			//3.根据分类id以及下属的分类,扩展分类查找商品
			//获取子分类
			$CateModel = D("Category"); 
			$tree = $CateModel->getChildren($cate_id);
			//添加父分类
			$tree[] = $cate_id;
			//转换为字符串
			$children = implode(",",$tree);
			//获取扩展分类的商品ID
			$ext_goods_ids = M("GoodsCate")->group("goods_id")->where("cate_id in ($children)")->select();

			if($ext_goods_ids){
				foreach ($ext_goods_ids as $key => $value) {
					$goods_ids[] = $value["goods_id"];
				}
				//转换为字符串
				$goods_ids = implode(",", $goods_ids);
			}
			//组合where条件
			if(!$goods_ids){
				$condition .= " AND cate_id in ($children)";
			}else{
				$condition .= " AND cate_id in ($children) OR id in ($goods_ids)";
			}
		}
		//上下架搜索
		$is_sale = intval(I("post.is_sale"));
		if($is_sale == 1){
			$condition .= " AND is_sale=1";
		}else if($is_sale === 2){
			$condition .= " AND is_sale=0";
		}
		//属性搜索
		$attribute = I("post.attribute");
		if($attribute){
			if($attribute == "is_hot" || $attribute == "is_rec" || $attribute == "is_new"){
				$condition .= " AND $attribute = 1";
			}
		}
		//关键字搜索
		$goods_name = I("post.goods_name");
		if($goods_name){
			$condition .= ' AND goods_name like "%'.$goods_name.'%"';
		}
		//测试查询条件
		//echo $condition;
		//获取总记录条数
		$count = $this->where($condition)->count();
		//创建分页类
		$Page = new \Think\Page($count,$pageSize);
		//获取分页导航数据
		$show = $Page->show();
		$data = $this->where($condition)->limit($Page->firstRow.",".$pageSize)->select();
		//返回数据以及分页导航
		return ["pageStr"=>$show,"data"=>$data];
	}
	public function setState($goods_id,$is_del=1){
		//修改状态
		return $this->where("id=".$goods_id)->setField("is_del",$is_del);
	}

	public function update($data){
		$goods_id = $data["id"];
		//货号问题
		$goods_sn = $data["goods_sn"];
		if(!$goods_sn){
			$data["goods_sn"] = "huohao".uniqid();
		}else{
			//有提交货号 并且将自己的货号排除在外
		 	$info = $this->where("goods_sn='$goods_sn' AND id !=$goods_id")->find();
		 	if($info){
		 		$this->error = "货号重复";
		 		return false;
		 	}
		}
		//扩展分类问题
		//删除之前的扩展分类 
		$cate_ext = D("GoodsCate");
		$res = $cate_ext->where("goods_id = $goods_id")->delete();
		$cate_id = I("post.cate_ext");
		$cate_ext->insertExtCate($goods_id,$cate_id);

		//上传图片以及制作缩略图（tp无法自己创建文件)
		$res = $this->uploadImg();
		if($res){
			$data["goods_img"] = $res["goods_img"];
			$data["goods_thumb"] = $res["thumb_goods_img"];
		}
		return $this->save($data);
	}
	public function uploadImg(){
		//判断是否图片上传
		if(isset($_FILES["goods_img"]) || $_FILES["goods_img"]["error"]!=0){
			return false;
		}
		//上传图片以及制作缩略图（tp无法自己创建文件)
		$upload = new \Think\Upload();
		$infos = $upload->uploadOne($_FILES["goods_img"]);
		if(!$infos){
			$this->getError();
		}
		//上传之后的图片地址
		$goods_img = "Uploads/".$infos["savepath"].$infos["savename"];
		//实现缩略图的制作
		$image = new \Think\Image();
		$image->open($goods_img);
		//缩略图路径
		$thumb_goods_img = "Uploads/".$infos["savepath"]."thumb_".$infos["savename"];
	
		$image->thumb(150,150)->save($thumb_goods_img);
		$data["goods_img"] = $goods_img;
		$data["goods_thumb"] = $thumb_goods_img;
		return ["goods_img"=>$goods_img,"goods_thumb"=>$thumb_goods_img];
	}
	public function dels($id){
		return $this->where("id=".$id)->delete();
	}
}
?>
